<?php
/* @var $this FaqController */
/* @var $model Faq */
/* @var $form TbActiveForm */
?>

<div class="form">

	<?php
	$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
		'id' => 'faq-form',
		// Please note: When you enable ajax validation, make sure the corresponding
		// controller action is handling ajax validation correctly.
		// There is a call to performAjaxValidation() commented in generated controller code.
		// See class documentation of CActiveForm for details on this.
		'enableAjaxValidation' => true,
		'clientOptions' => array(
			'validateOnSubmit' => true,
		),
	));
	?>


	<?php echo $form->errorSummary($model); ?> 	

	<?php
	foreach (Yii::app()->params['categories'] as $cat) 
	{
		if (!empty($cat['faqUrl'])) 
		{
			$cats[$cat['pageId']] = $cat['pageTitle'];
		}
	}
	echo $form->dropDownListControlGroup($model, 'faqCategoryId', $cats, array('empty' => '--',
		'ajax' => array(
			'type' => 'POST',
			'url' => '/x8/model2serv/getData',
			'data' => array(
				'id' => 'js:this.value',
				'to' => 'brand'
			),
			'update' => '#brand',
		))
	);
	?>

	<?php
	echo $form->dropDownListControlGroup($model, 'faqBrandId', $model->getData('brand', $model->faqCategoryId), array('empty' => '--', 'id' => 'brand',
		'ajax' => array(
			'type' => 'POST',
			'url' => '/x8/model2serv/getData',
			'data' => array(
				'id' => 'js:this.value',
				'to' => 'model'
			),
			'update' => '#source',
		))
	);
	?>

	<?php
	echo $form->dropDownListControlGroup($model, 'faqModelId', $model->getData('model', $model->faqBrandId), array('empty' => '--', 'encode' => false, 'id' => 'source'));
	?> 

	
	<?php echo $form->textFieldControlGroup($model, 'faqTitle', array('span' => 5, 'maxlength' => 255)); ?>

	<?php echo $form->textFieldControlGroup($model, 'faqUrl', array('span' => 5, 'maxlength' => 255)); ?>

	<?php echo $form->textAreaControlGroup($model, 'faqContent', array('span' => 8)); ?>	

	<?php echo $form->textFieldControlGroup($model, 'faqUserName', array('span' => 5, 'maxlength' => 255)); ?>

	<?php echo $form->textFieldControlGroup($model, 'faqUserEmail', array('span' => 5, 'maxlength' => 255)); ?>

	<?php echo $form->dropDownListControlGroup($model, 'faqStatus', array('Не опубликован', 'Опубликован')); ?>

	<?php //echo $form->checkBoxControlGroup($model, 'faqMostUseful', array('span' => 5));   ?>

	<script>
		$(document).ready(function()
		{
			$('#Faq_faqTitle').keyup(function()
			{
				$('#Faq_faqUrl').val(cyr2lat($(this).val(), mobi03.url));
			});
		});
	</script>
	<span class="inputname">
		Ответы:
		<a href="#" class="add_project_file">
			Добавить
		</a>
	</span>

	<table class="table table-hover no-more-tables project_images">
		<thead>
			<tr>							
				<th>Ответ</th>
				<th>Имя</th>
				<th>E-mail</th>
				<th>лучший ответ</th>
				<th></th>				
			</tr>
		</thead>
		<tbody>
			<tr class="answers">
				<td>
					<textarea name="answer[]"></textarea>
				</td>
				<td>
					<input type="text" name="name[]" value="<?php echo Yii::app()->user->isGuest ? '' : 'support'; ?>" />
				</td>
				<td>
					<input type="text" name="email[]" value="<?php echo Yii::app()->user->isGuest ? '' : Yii::app()->user->name['userEmail']; ?>" />
				</td>
				<td>
					<input type="radio" name="best" value="0" <?php if (!Yii::app()->user->isGuest && Yii::app()->user->name['userGroup'] == 'admin') echo 'checked="checked"'; ?> />
				</td>
				<td>
					<a href="#" class="remove_project_file">Удалить</a>
				</td>
			</tr>
<?php if (!$model->isNewRecord && !empty($answers)): ?>
	<?php foreach ($answers as $key => $answer): ?>
					<tr class="answers">
						<td>
							<textarea name="answer[]"><?php echo $answer['faqContent']; ?></textarea>
						</td>
						<td>
							<input type="text" name="name[]" value="<?php echo $answer['faqUserName']; ?>" />
						</td>
						<td>
							<input type="text" name="email[]" value="<?php echo $answer['faqUserEmail']; ?>" />
						</td>
						<td>
							<input type="radio" name="best" <?php echo $answer['faqMostUseful'] == 1 ? 'checked' : ''; ?> value="<?php echo $key + 1; ?>" />
						</td>
						<td>
							<a href="#" class="remove_project_file">Удалить</a>
						</td>
					</tr>
	<?php endforeach; ?>
<?php endif; ?>
		</tbody>
	</table>
	<script>
		$(document).ready(function() {
			$('.add_project_file').click(function(e) {
				e.preventDefault();
				var matches = 0;
				$("input[type=radio][name=best]").each(function(i, val) {
					matches++;
				});
				$(".project_images").append($('.answers:first').clone());
<?php echo!$model->isNewRecord ? "$('input[type=radio]:last-child').val(matches + 1);" : "$('input[type=radio]:last-child').val(matches);" ?>
			});
			$('.project_images').on('click', '.remove_project_file', function(e) {
				e.preventDefault();
				$(this).parent('td').parent('tr').remove();
			});
		});
	</script>
	<style>
		.answers:first-child {
			display: none;
		}
	</style>
	<div class="form-actions">
<?php
echo TbHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array(
	'color' => TbHtml::BUTTON_COLOR_PRIMARY,
	'size' => TbHtml::BUTTON_SIZE_LARGE,
));
?>
    </div>

		<?php $this->endWidget(); ?>

</div><!-- form -->